﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.IO;
class IniFile
{
    private Dictionary<string, Dictionary<string, string>> dic = new Dictionary<string, Dictionary<string, string>>();
    public void ReadFile(string file)
    {
        try
        {
            string[] array = File.ReadAllLines(file);//读取行
            string mainKey = "";
            foreach (var item in array)
            {
                string line = item.Trim();//去除首位空格
                if (line == "") continue;//跳过空行
                if (line.StartsWith("["))//判断是否为主键
                {
                    mainKey = line.Substring(1, line.Length - 2);       //读取file文件中的所有行
                    dic.Add(mainKey, new Dictionary<string, string>()); //创建一个mainKey对应的内层字典
                }
                else if (line.Contains("="))//判断是否为内层键值对
                {
                    string[] kv = line.Split('='); //拆出来键和值
                    dic[mainKey].Add(kv[0].TrimEnd(), kv[1].TrimStart());
                }
            }
        }
        catch (Exception)
        {

            Console.WriteLine("wrong");
        }
    }
    //获取字符串
    public string GetString(string mainKey, string subKey)
    {
        return dic[mainKey][subKey];
    }
    //获取整数
    public int GetInt(string mainKey, string subKey)
    {
        return int.Parse(GetString(mainKey, subKey));
    }
    //获取浮点数
    public float GetFloat(string mainKey, string subKey)
    {
        return float.Parse(GetString(mainKey, subKey));
    }

    //获取bool
    public bool GetBool(string mainKey, string subKey)
    {
        return bool.Parse(GetString(mainKey, subKey));
    }

}


